package org.lucky.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.lucky.admin.query.LuckyShareQuery;
import org.lucky.admin.service.ILuckyService;
import org.lucky.admin.service.IMenuService;
import org.lucky.admin.util.ExcelUtil;
import org.lucky_common.domain.InviteParam;
import org.lucky_common.domain.LuckyShare;
import org.lucky_common.util.AjaxResult;
import org.lucky_common.util.PageList;
import org.lucky_common.vo.LuckyShareExportVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/adminLucky")
public class AdminLuckyShareController {

    @Resource
    private ILuckyService luckyService;

    @PutMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody LuckyShare luckyShare) {
        boolean b = luckyService.saveOrUpdate(luckyShare);
        if (b){
            return AjaxResult.success();
        }else {
            return AjaxResult.error("Error saving employee");
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {

        boolean b = luckyService.removeById(id);
        if (b){
            return AjaxResult.success();
        }else {
            return AjaxResult.error("Error");
        }
    }

    @DeleteMapping("/deleteList")
    public AjaxResult deleteList(@RequestBody ArrayList<Long> ids) {
        System.out.println(ids);
        boolean b = luckyService.removeByIds(ids);
        if (b){
            return AjaxResult.success();
        }else {
            return AjaxResult.error("Error");
        }
    }

    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable("id") Long id) {
        return AjaxResult.success(luckyService.getById(id));
    }

    @PostMapping
    public AjaxResult queryPage(@RequestBody LuckyShareQuery luckyShareQuery) {
        Page<LuckyShare> page = new Page<LuckyShare>(luckyShareQuery.getPage(),luckyShareQuery.getRows(),true);
        if (luckyShareQuery.getStart().equals("") && luckyShareQuery.getEnd().equals("")){
            page = luckyService.page(page,new QueryWrapper<LuckyShare>().like("inviteCode",luckyShareQuery.getKeyword()).orderByDesc("data"));
            PageList<LuckyShare> pageList = new PageList<LuckyShare>(page.getTotal(),page.getRecords());
            return AjaxResult.success(pageList);
        }else {
            page = luckyService.page(page,new QueryWrapper<LuckyShare>().like("inviteCode",luckyShareQuery.getKeyword()).between("data",luckyShareQuery.getStart(),luckyShareQuery.getEnd()));
            PageList<LuckyShare> pageList = new PageList<LuckyShare>(page.getTotal(),page.getRecords());
            return AjaxResult.success(pageList);
        }

    }

    @GetMapping("/exportExcel")
    public void export(HttpServletResponse response) {
        //查询要导出的数据
        List<LuckyShareExportVo> users = luckyService.getUserExportList();
        LuckyShareExportVo.threadLocal_collectionSize.set( users.size() );
        LuckyShareExportVo.threadLocal_insertIndex.set( 1 );
        ExcelUtil.exportExcelX(users, "邀请阶段一信息表", "sheet1", LuckyShareExportVo.class, "邀请阶段一信息表.xlsx", response);
    }

    @PutMapping("/exportMore")
    public void exportMore11(@RequestBody ArrayList<LuckyShareExportVo> data,HttpServletResponse response) {
        List<LuckyShareExportVo> userExportVo = new ArrayList<>(data);
        LuckyShareExportVo.threadLocal_collectionSize.set( userExportVo.size() );
        LuckyShareExportVo.threadLocal_insertIndex.set( 1 );
        ExcelUtil.exportExcelX(userExportVo, "邀请阶段一信息表", "sheet1", LuckyShareExportVo.class, "邀请阶段一信息表.xlsx", response);
    }

    @GetMapping("/getCount")
    public AjaxResult getCount() {
        Map<String,List> count = luckyService.getCount();
        return AjaxResult.success(count);
    }

    @GetMapping("/getChartLine")
    public AjaxResult getChartLine(){
        List chartLine = luckyService.getChartLine();
        return AjaxResult.success(chartLine);
    }

}
